//
//  KLMessageListViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/30.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLMessageListViewController.h"
#import "KLMessageChatViewController.h"
#import "KLMessageChatViewController.h"
@interface KLMessageListViewController ()<EaseConversationListViewControllerDataSource,EaseConversationListViewControllerDelegate>

@end

@implementation KLMessageListViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息列表";
    self.showRefreshHeader = YES;
    self.dataSource = self;
    self.delegate = self;
    [self tableViewDidTriggerHeaderRefresh];
    [[EMClient sharedClient].chatManager removeDelegate:self];
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    //系统消息自定义
  //  self.tableView.tableHeaderView = header;
}
//- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
//    if (action == @selector(tableView:canEditRowAtIndexPath:)) {
//        return NO;//关闭删除，删除导致 的位置错乱，crash
//    }
//      return [super canPerformAction:action withSender:sender];
//}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}
#pragma mark --IEMChatManager
- (void)messagesDidReceive:(NSArray *)aMessages{
    EMMessage * message = aMessages.firstObject;
    NSDictionary * dic = message.ext;
    [self tableViewDidTriggerHeaderRefresh];
}
//解析消息体的 扩展消息
- (id<IConversationModel>)conversationListViewController:(EaseConversationListViewController *)conversationListViewController
                                    modelForConversation:(EMConversation *)conversation{
    
    id <IConversationModel> obj = nil;
    obj = [[EaseConversationModel alloc] initWithConversation:conversation];
    EMMessage * message = conversation.lastReceivedMessage;
    if ([message.ext.allKeys containsObject:@"name"]) {
        obj.title = message.ext[@"name"];
    }else{
        obj.title = @"客服";
    }
    if ([message.ext.allKeys containsObject:@"iconUrl"]) {
        obj.avatarURLPath = message.ext[@"iconUrl"];
    }else{
        obj.avatarImage = [UIImage imageNamed:@"touxiang"];
       // touxiang logo_release
    }

    return obj;
    
}
- (void)conversationListViewController:(EaseConversationListViewController *)conversationListViewController didSelectConversationModel:(id<IConversationModel>)conversationModel{


    KLMessageChatViewController * vc = [[KLMessageChatViewController alloc]
                                        initWithConversationChatter:conversationModel.conversation.conversationId
                                        conversationType:conversationModel.conversation.type];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
